let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.setStorageSync("code", res.code)
      }
    })
    wx.request({
      url: app.data.url + '/passwordIsSet',
      method: "POST",
      header: app.getHeader(),
      success: function(res) {
        if (res.data.tips == 200) {
          //on
          app.data.passSet = true
        } else {
          //off
          app.data.passSet = false
        }
      }
    })
    wx.request({
      url: app.data.url + '/isrz',
      method: "POST",
      header: app.getHeader(),
      success: function(res) {
        if (res.data.tips == 200) {
          //on
          app.data.hasRenzheng = true
        } else {
          //off
          app.data.hasRenzheng = false
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  userInfoHandler: function(res) {
    var that = this;
    if (this.userInfoReadyCallback) {
      this.userInfoReadyCallback(res)
    }
    wx.setStorageSync("userInfo", res.detail.userInfo)
    if (wx.getStorageSync("userInfo")) {

      wx.request({
        url: app.data.url + '/get_code',
        method: 'POST',
        data: {
          code: app.data.code,
          nickName: wx.getStorageSync("userInfo").nickName,
          avatarUrl: wx.getStorageSync("userInfo").avatarUrl,
          gender: wx.getStorageSync("userInfo").gender,
        },
        success: function(res) {
          app.data.openid = res.data.openid
          wx.setStorageSync('openid', res.data.openid)

          // app.data.openid = "o7-lc5UM-ClUHY3ymXMm5qhwH0M0"
          // wx.setStorageSync('openid', "o7-lc5UM-ClUHY3ymXMm5qhwH0M0")
          wx.request({
            url: app.data.url + '/is_perfect',
            method: "GET",
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'openid': res.data.openid
            },
            success: function(res) {
              if (res.data == 1) {
                wx.switchTab({
                  url: '../sports/sports'
                })
              } else {
                wx.redirectTo({
                  url: '../login/login?openid=' + res.data.openid
                })
              }
            }
          })
        },
        fail: function(err) {}
      })
    }
  },
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /* 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})